﻿using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.ExpressApp.DC;
using DevExpress.Persistent.Base;
using DevExpress.Xpo;
using DevExpress.Persistent.Validation;
using DevExpress.ExpressApp;
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp.SystemModule;
using DevExpress.ExpressApp.Actions;

namespace DomainComponents.XCRM {
    public enum ProductStatus {
        Inactive = 0,
        Active = 1
    }

    public class ProductValidationRules {
        public const string NameIsRequired = "ProductNameIsRequired";
        public const string PriceIsGreaterThanOrEqualZero = "PriceIsGreaterThanOrEqualZero";
    }

    [DomainComponent, XafDefaultProperty("Name")]
    [VisibleInReports]
    [ImageName("BO_Product")]
    [ListViewFilter("Product_ActiveProducts", "Status='Active'", "Active Products", true, Index = 0)]
    [ListViewFilter("Product_AllProducts", "", "All Products", Index = 1)]
    [ListViewFilter("Product_InactiveProducts", "Status='Inactive'", "Inactive Products", Index = 2)]
    [XafDisplayName("Product")]
    public interface IProduct {
        [RuleRequiredField(ProductValidationRules.NameIsRequired, DefaultContexts.Save)]
        string Name { get; set; }

        [RuleValueComparison(ProductValidationRules.PriceIsGreaterThanOrEqualZero, DefaultContexts.Save, ValueComparisonType.GreaterThanOrEqual, 0)]
        decimal Price { get; set; }
        
        [System.ComponentModel.DefaultValue(ProductStatus.Active)]
        ProductStatus Status { get; set; }
        
        ICategory Category { get; set; }
        
        [Action(PredefinedCategory.Edit, Caption="Activate Product...",AutoCommit=true, 
            ConfirmationMessage="This operation will set the selected Product as Active.", TargetObjectsCriteria="Status != 'Active'",
            SelectionDependencyType = MethodActionSelectionDependencyType.RequireMultipleObjects, ImageName = "Action_Workflow_Activate")]
        void Activate();
        
        [Action(PredefinedCategory.Edit, Caption = "Deactivate Product...", AutoCommit = true,
            ConfirmationMessage = ProductLogic.DeactivateConfirmationMessage, TargetObjectsCriteria = "Status = 'Active'",
            SelectionDependencyType = MethodActionSelectionDependencyType.RequireMultipleObjects, ImageName = "Action_Workflow_Deactivate")]
        void Deactivate();
    }

    [DomainLogic(typeof(IProduct))]
    public class ProductLogic {
        public const string DeactivateConfirmationMessage = 
@"This action will set the Product as inactive. There may be records in the system that continue to reference these inactive records.

Do you want to proceed?";

        public static void Deactivate(IProduct product) {
            product.Status = ProductStatus.Inactive;
        }
        public static void Activate(IProduct product) {
            product.Status = ProductStatus.Active;
        }
    }
}

